package com.example.controller;

import com.example.pojo.Friend;
import com.example.service.IFriendService;
import com.example.util.R;
import com.example.vo.UserFriend;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/friends")
@Api(tags = "交友api接口")
public class FriendController {

    @Autowired
    private IFriendService friendService;

    @ApiOperation("新增好友")
    @PostMapping("/save")
    public R<Friend> addFriend(@RequestBody Friend friend){
        try {
            friendService.save(friend);
            return R.success("好友添加成功！", friend);
        } catch (Exception e) {
            return R.fail("用户添加失败：" + e.getMessage());
        }
    }

    @ApiOperation("删除好友")
    @GetMapping("/remove/{id}")
    public R removeFriendById(@PathVariable("id") Long friendId){
        try {
            friendService.removeById(friendId);
            return R.success("用户删除成功", null);
        } catch (Exception e) {
            return R.fail("用户删除失败：" + e.getMessage());
        }
    }

    @ApiOperation("修改好友信息")
    @PostMapping("/update")
    public R<Friend> updateUser(@RequestBody Friend friend){
        try {
            friendService.updateById(friend);
            return R.success("用户修改成功！", friend);
        } catch (Exception e) {
            return R.fail("用户修改失败：" + e.getMessage());
        }
    }

    @ApiOperation("查询单个好友")
    @GetMapping("/list/{userId}")
    public R<List<UserFriend>> getFriendList(@PathVariable("userId") Long userId) {
        List<UserFriend> friends = friendService.getFriendList(userId);
        return R.success("查询成功", friends);
    }

    @ApiOperation("查询好友列表")
    @PostMapping("/lists")
    public R<List<UserFriend>> getFriends() {
        List<UserFriend> friends = friendService.getAllFriends();
        return R.success("查询成功", friends);
    }

    // 定义其他处理HTTP请求的控制器方法，例如添加好友请求、处理好友状态变更等

}
